Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

groq-builder: implement "conditionals" feature #252

Merged
merged 46 commits into from
Jan 18, 2024
Merged

Conversation

scottrippey
Copy link
Member

@scottrippey scottrippey commented Dec 27, 2023

What

Adds methods to create "conditional" projections.

Docs: (copied from CONDITIONALS.md)

Conditionals

In Groq, there are 2 ways to use conditional logic: inline in a projection, or using the select function.

Conditions in a projection

In groq-builder, the project method allows inline conditional statements with the help of q.conditional$(...) or q.conditionalByType(...) using the following syntax:

const contentQuery = q.star
  .filterByType("movie", "actor")
  .project({
    slug: "slug.current",
    ...q.conditional$({
      "_type == 'movie'": { title: "title", subtitle: "description" },
      "_type == 'actor'": { title: "name", subtitle: "biography" },
    }),
  });

This outputs the following groq query:

*[_type == "movie" || _type == "actor"] {
  "slug": slug.current,
  _type == 'movie' => {
    title,
    "subtitle": description
  },
  _type == 'actor' => {
    "title": name,
    "subtitle": biography
  }
}

And the result type is inferred as:

type ContentResults = InferResultType<typeof contentQuery>;
// Same as:
type ContentResults = Array<
  | { slug: string }
  | { slug: string, title: string, subtitle: string }
>;

Notice that the conditions are wrapped in q.conditional$() and then spread into the projection. This is necessary for type-safety and runtime validation.

The $ in the method q.conditional$ indicates that this method is not completely type-safe; the condition statements (eg. _type == 'movie') are not strongly-typed (this may be improved in a future version).

However, the most common use-case is to base conditional logic off the document's _type. For this, we have the q.conditionalByType helper:

Strongly-typed conditions via q.conditionalByType(...)

The most common use-case for conditional logic is to check the _type field.
The q.conditionalByType(...) method makes this easier, by ensuring all conditional logic is strongly-typed, and it enables auto-complete. For example:

const contentQuery = q.star
  .filterByType("movie", "actor")
  .project(q => ({
    slug: "slug.current",
    ...q.conditionalByType({
      movie: { title: "title", subtitle: "description" },
      actor: { title: "name", subtitle: "biography" },
    })
  }));

The resulting query is identical to the above example with q.conditional$.

The result type here is inferred as:

type ContentResult = InferResultType<typeof contentQuery>;
// Same as:
type ContentResult = Array<
  { slug: string, title: string, subtitle: string }
>

Notice that this type is stronger than the example with q.conditional$, because we've detected that the conditions are "exhaustive".

The select method

Adds support for the select$ method:

const qMovies = q.star.filterByType("movie").project({
  name: true,
  popularity: q.select$({
    "popularity > 20": q.value("high"),
    "popularity > 10": q.value("medium"),
  }, q.value("low")),
});

The $ sign is to indicate that there's some "loosely typed" code in here -- the conditions are unchecked.

This will output the following query:

*[_type == "movie"] {
  name,
  "popularity": select(
    popularity > 20 => "high",
    popularity > 10 => "medium",
    "low"
  )
}

And will have the following result type:

type MoviesResult = InferResultType<typeof qMovies>;
// Same as:
type MoviesResult = Array<{
  name: string
  popularity: "high" | "medium" | "low"
}>

The selectByType method

Adds a selectByType helper, which facilitates type-based logic. This is completely strongly-typed:

const qContent = q.star.filterByType("movie", "actor").project(q => ({
  name: q.selectByType({
    movie: q => q.field("title"),
    actor: q => q.field("name"),
  })
}));

Copy link

vercel bot commented Dec 27, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
groqd ⬜️ Ignored (Inspect) Visit Preview Jan 17, 2024 11:41pm

scottrippey and others added 10 commits December 28, 2023 14:31
* feature(fragment): added `q.fragment` implementation

* feature(fragment): added tests for fragment queries

* feature(fragment): ensure we export `Fragment` and `InferFragmentType` types

* feature(fragment): added docs

* feature(validation): fixed broken import

---------

Co-authored-by: scottrippey <scott.william.rippey@gmail.com>
@scottrippey scottrippey marked this pull request as ready for review January 17, 2024 17:07
scottrippey added 5 commits January 17, 2024 11:19
# Conflicts:
#	packages/groq-builder/src/commands/fragment.test.ts
#	packages/groq-builder/src/commands/projection-types.ts
#	packages/groq-builder/src/types/public-types.ts
#	packages/groq-builder/src/types/utils.ts
Copy link
Contributor

@maxyinger maxyinger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have time to fully go through and understand the internal of this atm. But the outward facing api looks awesome and glancing through the test cases it all looks good to me. Awesome work

packages/groq-builder/docs/CONDITIONALS.md Show resolved Hide resolved
@scottrippey scottrippey merged commit 444704c into validation Jan 18, 2024
5 checks passed
@scottrippey scottrippey deleted the conditionals branch January 18, 2024 04:11
@scottrippey scottrippey restored the conditionals branch January 18, 2024 04:11
@scottrippey scottrippey deleted the conditionals branch January 18, 2024 14:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants